package com.carlinfo.shop01.goods.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.carlinfo.shop01.common.util.ConstatFinalUtil;
import com.carlinfo.shop01.common.util.PageInfoUtil;
import com.carlinfo.shop01.goods.dao.IAGoodsBrandDao;
import com.carlinfo.shop01.goods.pojo.AGoodsBrand;
import com.carlinfo.shop01.goods.service.IGoodsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service("goodsService")
public class GoodsServiceImpl implements IGoodsService
{
	@Resource
	private IAGoodsBrandDao goodsBrandDao;

	/**
	 * { // 响应码 "code":"0", // 响应信息 "info":"", // 数据 "data": { //
	 * 影响条数;sql执行以后对数据库的影响条数 "effect":"", // id,保存的时候要主键 "id":"" } }
	 */
	@Override
	public JSONObject saveOneGoodsBrandService(AGoodsBrand goodsBrand)
	{
		JSONObject resultJSON = new JSONObject();
		/* 保存 */
		int res = this.goodsBrandDao.saveOne(goodsBrand);

		/* 抛空指针,就是为了测试事务管理器 */
		/*
		 * String str = null ; str.toString() ;
		 */

		if (res > 0)
		{
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("effect", res);
			dataJSON.put("id", goodsBrand.getId());

			resultJSON.put("data", dataJSON);
			/* 成功 */
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		} else
		{
			/* 失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneGoodsBrandService(AGoodsBrand goodsBrand)
	{
		JSONObject resultJSON = new JSONObject();
		/* 保存 */
		int res = this.goodsBrandDao.updateOne(goodsBrand);

		/* 抛空指针,就是为了测试事务管理器 */
		/*
		 * String str = null ; str.toString() ;
		 */

		if (res > 0)
		{
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("effect", res);
			dataJSON.put("id", goodsBrand.getId());

			resultJSON.put("data", dataJSON);
			/* 成功 */
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		} else
		{
			/* 失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneGoodsBrandService(Map<String, Object> condMap)
	{
		return null;
	}

	@Override
	public AGoodsBrand findOneGoodsBrandService(Map<String, Object> condMap)
	{
		return this.goodsBrandDao.findOne(condMap);
	}

	@Override
	public Map<String, Object> findCondListService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		Map<String, Object> resultMap = new HashMap<String, Object>();
		/* 为关键字加上模糊匹配 */
		if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}

		if (pageInfoUtil != null)
		{
			/* 分页 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AGoodsBrand> goodsBrandList = this.goodsBrandDao.findList(condMap);
			/* 一定要把总记录数放到pageInfoUtil中 */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			resultMap.put("goodsBrandList", goodsBrandList);
			resultMap.put("totalRecord", Integer.valueOf(page.getTotal() + ""));
		} else
		{
			List<AGoodsBrand> goodsBrandList = this.goodsBrandDao.findList(condMap);
			resultMap.put("goodsBrandList", goodsBrandList);
		}
		return resultMap;
	}
}
